package com.mv.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mv.mapper.RechargeMapper;
import com.mv.pojo.Card;
import com.mv.pojo.Deal;
import com.mv.pojo.Recharge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RechargeServiceImpl implements RechargeService{
    @Autowired
    private DealService dealService;
    @Autowired
    private RechargeMapper rechargeMapper;
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat dealTimeFormat = new SimpleDateFormat("HH:mm:ss");
    @Transactional
    @Override
    public void deleteRechargeByDealId(Integer dealId) {
        QueryWrapper<Recharge> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deal_id", dealId);
        rechargeMapper.delete(queryWrapper);
    }
    @Override
    public List<Recharge> getRechargeList(){
        int i=0;
        List<Recharge> rechargeList = new ArrayList<>();
        Map<Date, List<Recharge>> map = getMap();
        for (Date date : map.keySet()) {
            rechargeList.add(new Recharge().setId(i--).setDate(date).setChildren(map.get(date)));
        }
        return rechargeList;
    }
    /*key为日期,封装成map*/
    private Map<Date,List<Recharge>> getMap() {
        Map<Date,List<Recharge>> map=new HashMap<>();
        List<Recharge> rechargeList=findRechargeList();
        for (Recharge recharge : rechargeList) {
            Date date = recharge.getDate();
            if(map.containsKey(date)){
                map.get(date).add(recharge);
            }else {
                ArrayList<Recharge> list = new ArrayList<>();
                list.add(recharge);
                map.put(date,list);
            }
        }
        return map;
    }
    /*获取所有充值记录*/
    private List<Recharge> findRechargeList() {
        Date date = null;
        Date dealTime = null;
        List<Recharge> rechargeList = rechargeMapper.selectList(null);
        for (Recharge recharge : rechargeList) {
            String userName = dealService.getUserNameById(recharge.getDealId());
            Deal deal = dealService.getDealById(recharge.getDealId());
            String[] json = format.format(deal.getDealTime()).split(" ");
            try {
                date = dateFormat.parse(json[0]);
                dealTime = dealTimeFormat.parse(json[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            recharge.setCardId(deal.getCardId())
                    .setPrice(deal.getMoneyChange())
                    .setUserName(userName)
                    .setDate(date)
                    .setDealTime(dealTime);
        }
        return rechargeList;
    }

}
